package com.leetcode.bfs;

public class Leetcode695 {
    public int maxAreaOfIsland(int[][] grid) {
        int maxArea = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    int curArea = dfs(i, j , grid);
                    maxArea = Math.max(maxArea, curArea);
                }
            }
        }
        return maxArea;
    }

    private int dfs(int r, int c, int[][]grid) {
        if (notInArea(grid, r, c)) {
            return 0;
        }

        if (grid[r][c] != 1) {
            return 0;
        }
        grid[r][c] = 2;

        return  1
                + dfs(r - 1, c, grid)
                + dfs(r + 1, c, grid)
                + dfs(r, c - 1, grid)
                + dfs(r, c + 1, grid);

    }

    private boolean notInArea(int[][]grid, int r, int c) {
        return r < 0 || r >= grid.length || c < 0 || c >= grid[0].length;
    }
}
